package com.personal.system.controller.approval;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.personal.common.result.Result;
import com.personal.system.domain.ApprovalProcessConstants;
import com.personal.system.domain.EnumInfo;
import com.personal.system.entity.approval.AuditRule;
import com.personal.system.entity.approval.AuditRuleVo;
import com.personal.system.service.act.AuditRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.web.bind.annotation.*;
import org.xml.sax.SAXParseException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批规则Controller
 */
@Api(tags = "审批规则Controller")
@RestController
@RequestMapping("/rule")
public class AuditRuleController {

    @Resource
    private AuditRuleService auditRuleService;

    /**
     * 查询审批规则列表
     */
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable int pageNum, @PathVariable int pageSize,AuditRule auditRule) {
        Page<AuditRule> pageParam = new Page<>(pageNum, pageSize);

        IPage<AuditRule> list = auditRuleService.selectAuditRuleList(pageParam, auditRule);
        return Result.ok(list);
    }

    @ApiOperation("获取树型枚举")
    @GetMapping(value = "/getRuleEnum")
    public Result getRuleEnum() {

        ApprovalProcessConstants.ApprovalModuleEnum[] moduleV = ApprovalProcessConstants.ApprovalModuleEnum.values();
        List<Object> infos = Lists.newArrayList();
        for (ApprovalProcessConstants.ApprovalModuleEnum approval : moduleV) {
            EnumInfo enumInfo = new EnumInfo();
            enumInfo.setCode(approval.getCode());
            enumInfo.setName(approval.getName());
            List<EnumInfo> rules = Lists.newArrayList();
            for (ApprovalProcessConstants.RuleClassEnum rule : approval.getRules()) {
                EnumInfo info = new EnumInfo();
                info.setCode(approval.getCode() + "-" + rule.getCode());
                info.setName(rule.getName());

                List<ApprovalProcessConstants.AuditTypeEnum> auditTypes = ApprovalProcessConstants.AuditTypeEnum.getByType(rule.getCode());
                List<EnumInfo> audits = Lists.newArrayList();
                for (ApprovalProcessConstants.AuditTypeEnum auditType : auditTypes) {
                    EnumInfo audit = new EnumInfo();
                    audit.setCode(auditType.getCode());
                    audit.setName(auditType.getName());
                    audits.add(audit);
                }
                info.setChildren(audits);
                rules.add(info);
            }
            enumInfo.setChildren(rules);
            infos.add(enumInfo);
        }
        return Result.ok(infos);
    }

    @ApiOperation("获取部门角色用户")
    @GetMapping(value = "/getDeptRoleUser")
    public Result getDeptRoleUser() {

        return Result.ok(auditRuleService.getDeptRoleUser());
    }

    /**
     * 获取审批规则详细信息
     */
    @GetMapping(value = "/details/{ruleId}")
    public Result getInfo(@PathVariable("ruleId") String ruleId) {

        return Result.ok(auditRuleService.selectAuditRuleById(ruleId));
    }

    /**
     * 新增审批规则
     */
    @PostMapping("/addRule")
    public Result add(@RequestBody AuditRuleVo auditRuleVo) {
        try {
            return Result.ok(auditRuleService.insertAuditRule(auditRuleVo));
        } catch (Exception e) {
            if (e instanceof SAXParseException){
                return Result.ok();
            }
            return Result.fail().message("新增审批规则失败：" + e.getMessage());
        }
    }

    /**
     * 修改审批规则
     */
    @PostMapping("/updateRule")
    public Result updateRule(@RequestBody AuditRuleVo auditRuleVo) {

        try {
            return Result.ok(auditRuleService.updateAuditRule(auditRuleVo));
        } catch (Exception e) {
            if (e instanceof SAXParseException){
                return Result.ok();
            }
            return Result.fail().message("修改审批规则失败：" + e.getMessage());
        }
    }

    /**
     * 修改审批规则状态
     */
    @PutMapping("/updateState")
    public Result updateState(@RequestBody AuditRule auditRule) {

        return Result.ok(auditRuleService.updateState(auditRule));
    }

    /**
     * 删除审批规则
     */
    @DeleteMapping("/delete")
    public Result remove(@RequestBody String[] ids) {

        return Result.ok(auditRuleService.deleteAuditRuleByIds(ids));
    }

    /**
     * 查看审批明细
     */
    @GetMapping("/appDetails/{ruleId}")
    public Result appDetails(@PathVariable String ruleId) {

        return Result.ok(auditRuleService.appDetails(ruleId));
    }

}
